//自定义异常中间件
const result = require('./resultData')

let abnormal = async (ctx, next) => {
    try {
        await next();
    } catch (err) {
        console.log('捕获到异常')
        const isResult = err instanceof result
        if (isResult) {
            console.log('已知错误');
            ctx.body = {
                msg: err.msg
            }
            ctx.status = err.code;
        }else {
            ctx.body = {
                msg: '服务器发生错误'
            }
            ctx.status = 500;
        }
    }
}

module.exports = abnormal
